package com.example.weibo_suhuanzhen.activities;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.weibo_suhuanzhen.adapters.CollectionPagerAdapter;
import com.example.weibo_suhuanzhen.fragments.HomeFragment;
import com.example.weibo_suhuanzhen.fragments.LoggedInFragment;
import com.example.weibo_suhuanzhen.R;
import com.example.weibo_suhuanzhen.UserSession;
import com.example.weibo_suhuanzhen.sharedPreferences.SharedPreferencesManager;

import java.util.Objects;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private final HomeFragment homeFragment=new HomeFragment();
    private final LoggedInFragment myFragment=new LoggedInFragment();
    private TextView tv_home;
    private TextView tv_my;
    private ImageView im_home;
    private ImageView im_my;
    private ViewPager pager;

    private SharedPreferencesManager preferences;
    private CollectionPagerAdapter collectionPagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        getSupportFragmentManager().beginTransaction()
                .setReorderingAllowed(true)
                .add(R.id.pager, myFragment)
                .hide(myFragment)
                .commit();
        getSupportFragmentManager().beginTransaction()
                .setReorderingAllowed(true)
                .add(R.id.pager, homeFragment)
                .commit();
        collectionPagerAdapter =new CollectionPagerAdapter(getSupportFragmentManager());
        pager = findViewById(R.id.pager);
        pager.setAdapter(collectionPagerAdapter);
        initView();


    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @SuppressLint("UseCompatLoadingForDrawables")
    private void initView(){
        preferences = SharedPreferencesManager.getInstance(this);
        tv_home = findViewById(R.id.home_tv);
        tv_my = findViewById(R.id.my_tv);
        im_home = findViewById(R.id.logo_home);
        im_my = findViewById(R.id.logo_my);
        im_home.setBackground(getResources().getDrawable(R.drawable.home_2));
        tv_home.setOnClickListener(this);
        tv_my.setOnClickListener(this);
        pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                // Not used in this case
            }

            @Override
            public void onPageSelected(int position) {
                // 根据当前的项来更新导航栏的状态
                if (position == 0) {
                    // 当前项是主页
                    im_home.setBackground(getResources().getDrawable(R.drawable.home_2));
                    im_my.setBackground(getResources().getDrawable(R.drawable.mine_1));
//                    Fragment currentFragment = getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.pager + ":" + pager.getCurrentItem());
//                    if (currentFragment instanceof HomeFragment) {
//                        ((HomeFragment) currentFragment).refreshData();
//                    }

                } else if (position == 1) {
                    // 当前项是我的
                    im_home.setBackground(getResources().getDrawable(R.drawable.home_1));
                    im_my.setBackground(getResources().getDrawable(R.drawable.mine_2));
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                // Not used in this case
            }
        });

    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.home_tv:
                pager.setCurrentItem(0);  // 切换到主页片段
                break;
            case R.id.my_tv:
                pager.setCurrentItem(1);
                break;

        }
    }

}